package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.utils.BaseContest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    /**
     * 用户下单 保存订单表 订单
     * @param orders
     * @return
     */
    @PostMapping("submit")
  public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("");
    }

    /**
     * 订单分页
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("page")
    public R<Page<Orders>> page(Integer page , Integer pageSize , String number, String beginTime, String endTime){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.query().like(StringUtils.isNotBlank(number), "number", number)
                .between(beginTime !=null,"order_time", beginTime, endTime)
                .orderByAsc("order_time")
                .page(ordersPage);
        return R.success(ordersPage);
    }

    /**
     * 修改订单信息
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        ordersService.update().eq("id",orders.getId())
                .set(orders.getStatus()!=null,"status",orders.getStatus())
                .update();
        return R.success("更新成功");
    }
    /**
     * 查询历史订单
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> pageOrdersDto(Integer page, Integer pageSize){
        Long aLong = BaseContest.get();
        Page<OrdersDto> ordersDtoPage=ordersService.pageOrdersDto(page,pageSize,aLong);
        return R.success(ordersDtoPage);
    }
}
